$(function(){
	var detailId;
	provinceInfo.baseURL = baseURL;
	provinceInfo.divId = "test";
	provinceInfo.empty = true;
	provinceInfo.init();
	provinceInfo.bindEvent();
	$("#form-add").find('tr td:first-child').css('font-weight','bold');
	$("#form-add").find('tr td:nth-child(3)').css('font-weight','bold');
	$.ajax({
		type : "get",
		url : baseURL+'/api/organization/get/' + orgId+'?times='+(new Date()).getTime(),
		dataType : "json",
		timeout : 30000,
		contentType : "application/json",
		success : function(result) {
			if (result.flag) {
				var result=result.result;
				var liceTime = "";
				if(result.liceTime){
					var date = new Date();
					date.setTime(result.liceTime);
					liceTime = date.pattern("yyyy-MM-dd");
			     }
				$('#orgInfo').html("<div class='orgBaseInfo'>基础资料</div><table class='orgBaseInfoTable table table-bordered table-border table-hover table-striped'>" +"<tbody class='text-l'><tr><td>机构名称</td><td>"+isNull(result.name)+"</td><td>机构代码</td><td>"+isNull(result.code)+"</td></tr><tr><td>区县行政区</td><td>"+isNull(result.provinceName)+" "+isNull(result.cityName)+" "+isNull(result.districtName) +"</td><td>简称</td>" +"<td>"+isNull(result.shortName)+"</td></tr><tr><td>经营许可证编号</td><td>"+isNull(result.licNum)+"</td><td>经营许可日期</td><td>"+isNull(liceTime)+"</td>" +"</tr><tr><td>营业执照注册号</td><td>"+isNull(result.business)+"</td><td>统一社会信用代码</td><td>"+isNull(result.creditCode)+"</td></tr><tr><td>法人代表</td><td  colspan=3>"+isNull(result.legal)+"</td></tr><tr><td>联系人</td><td>"+isNull(result.contact)+"</td><td>联系电话</td><td>"+isNull(result.phone)+"</td></tr>" +"<tr><td>机构地址</td><td>"+isNull(result.address)+"</td><td>邮政编码</td><td>"+isNull(result.postCode)+"</td></tr></tbody></table><div class='col-4 col-offset-5 mt-20'><span class='l'><button type='btn' class='btn btn-primary radius'>修改信息</button></span></div>");
				$('#name').val(result.name);
				$('#district').val(result.district);
				$('#shortname').val(result.shortName);
				$('#licnum').val(result.licNum);
				$('#licetime').val(liceTime);
				$('#business').val(result.business);
				$('#creditcode').val(result.creditCode);
				$('#code').val(result.code);
				$('#legal').val(result.legal);
				$('#contact').val(result.contact);
				$('#phone').val(result.phone);
				$('#address').val(result.address);
				$('#postcode').val(result.postCode);
				$('#_province').attr("name",result.provinceCode).val(result.provinceName);
				$('#_city').attr("name",result.cityCode).val(result.cityName);
				$('#_town').attr("name",result.districtCode).val(result.districtName);
				$('body').find('button:last').on('click',function(e){
					$("#orgInfo").hide()
					$("#form-add").show();
				})
			}
			$("#orgInfo").find('tr td:first-child').css('font-weight','bold');
			$("#orgInfo").find('tr td:nth-child(3)').css('font-weight','bold');
		}
	});  
	function isNull(a){
		if(a==null){
			return "";
		}else return a;
	}
	$("#form-add").Validform({
		tiptype : 1,
		showAllError:true,
		datatype: {
			"empty": /^\s*$/,
			"code":/^[a-z0-9A-Z_-]{2,16}$/,
			"money" : /^[0-9]\d{0,5}$/
				},
		callback : function(form) {
			layer.confirm('您确定要提交表单吗?',function(){
				try{
					submit();
					}catch(e){
						layer.msg(e);
					}
			})
						return false;
			}

	});
	submit = function(){
		var data = {};
		data.name = $('#name').val();
		data.provinceCode = $('#_province').attr('name')!=""?$('#_province').attr('name'):null;
		data.cityCode = $('#_city').attr('name')!=""?$('#_city').attr('name'):null;
		data.districtCode = $('#_town').attr('name')!=""?$('#_town').attr('name'):null;
		data.provinceName=$('#_province').val();
		data.cityName=$('#_city').val();
		data.districtName=$('#_town').val();
		data.shortName = $('#shortname').val();
		data.licNum = $('#licnum').val();
		data.liceTime = $('#licetime').val();
		data.business = $('#business').val();
		data.creditCode = $('#creditcode').val();
		data.code =$('#code').val();
		data.legal = $('#legal').val();
		data.contact = $('#contact').val();
		data.phone=$('#phone').val();
		data.address=$('#address').val();
		data.postCode=$('#postcode').val();
		data.id=orgId;
		/*function addProvince() {
			var addProvinceValue;
			$.each($('#_province').find('option'), function() {
				if ($(this).text() == $('#_province').next().find('input').val()) {
					addProvinceValue = $(this).val();
				}
			})
			return addProvinceValue;
		}
		function addCity() {
			var addCityValue;
			$.each($('#_city').find('option'), function() {
				if ($(this).text() == $('#_city').next().find('input').val()) {
					addCityValue = $(this).val();
				}
			})
			return addCityValue;
		}
		function addTown() {
			var addTownValue;
			$.each($('#_town').find('option'), function() {
				if ($(this).text() == $('#_town').next().find('input').val()) {
					addTownValue = $(this).val();
				}
			})
			return addTownValue;
		}*/
		$.ajax({
			type : "post",
			url : baseURL + '/api/organization/udpate?times='+(new Date()).getTime(),
			dataType : "json",
			timeout : 30000,
			contentType : "application/json",
			data : JSON.stringify(data),
			success : function(result) {
				if (!result.flag) {
					layer.msg("新增失败！原因：" + result.message);
					return;
				}
				if(isManagerPlat=="true"){
					window.location.href=baseURL + '/api/organization/page/list?times='+(new Date()).getTime();
				}else{
					window.location.href=baseURL + '/api/organization/page/detailinfo?times='+(new Date()).getTime();
				}			
			}
		}); 
	 }
})



